package com.jshan.blog.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshan.blog.annotation.OperationLog;
import com.jshan.blog.base.admin.dto.SysLogQueryDto;
import com.jshan.blog.base.admin.entity.SysOperationLog;
import com.jshan.blog.enums.BizCodeEnums;
import com.jshan.blog.result.Result;
import com.jshan.blog.service.OperationLogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/log")
public class SysLogController {

    @Resource
    private OperationLogService logService;

    @PostMapping("/getLogList/{page}/{pageSize}")
    // @OperationLog(value = "分页查询日志列表，也可以根据条件筛选", operationType = "查询")
    @SaCheckPermission(value = {"sys:log:manage"})
    public Result getLogList(@PathVariable("page") Integer page,
                             @PathVariable("pageSize") Integer pageSize,
                             @RequestBody SysLogQueryDto sysLogQueryDto) {
        Page<SysOperationLog> logPage = logService.pageAll(new Page<SysOperationLog>(page, pageSize), sysLogQueryDto);
        // log.info("getRecords{}",logPage.getRecords().toString());
        Map<Object, Object> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(logPage.getRecords())) {
            map.put("records", logPage.getRecords());
            map.put("total", logPage.getTotal());
            return Result.ok(map);
        }
        return Result.fail(BizCodeEnums.FAILE.getCode(), "数据不存在! 或者获取日志信息失败！");
    }
}
